package com.manager.core.shiro.core.service;

import java.util.*;

import com.manager.core.shiro.core.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.core.shiro.core.dao.ShiroDao;

/**
 * ShiroService
 *
 * @author H
 */
@Service
public class ShiroService extends GenericService<ShiroPermission> {

    @Autowired
    protected ShiroDao jdbcDao;

    @Override
    public GenericDao<ShiroPermission> getDao() {
        return jdbcDao;
    }

    @Override
    public ShiroPermission getModel() {
        return new ShiroPermission();
    }


    public UserView queryUserView(String account) {
        return jdbcDao.queryUserView(account);
    }

    public LoginUser queryAccountInfo(String account) {
        LoginUser loginUser = jdbcDao.login(new HashMap<String, Object>() {
            {
                put("account", account);
            }
        });
        return loginUser;
    }

    /**
     * 登录
     *
     * @param account
     * @param pwd
     * @param ip
     * @return
     */
    public LoginUser login(final String account, String pwd, final String ip) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        if (StringUtils.isBlank(pwd)) {
            return null;
        }
        try {
            LoginUser loginUser = jdbcDao.login(new HashMap<String, Object>() {
                private static final long serialVersionUID = 623012754587947505L;
                {
                    put("account", account);
                }
            });
            if (null != loginUser) {
                if (pwd.equals(loginUser.getPassword())) {
                    return new LoginUser(loginUser.getAccount(), loginUser.getPassword());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
}